import model
import datetime
import resource.string.app

from _console import Console

class Repairer(Console):
    """Konata Repairer

    Repair tag by prepend and append space.
    """

    def run(self):
        """Main program"""

        self.greet(resource.string.app)

        self.repairTag()

        self.farewell()

    def repairTag(self):
        """Repair tag from post table."""

        db = self.openDb()
        posts = db.query(model.Post)
        for index, post in enumerate(posts):
            print index
            if post.tag:
                tag = ' %s ' % post.tag.strip()
                if post.tag != tag: post.tag = tag

        if db.dirty: db.commit()
